package com.cnap.service.impl;

import com.cnap.db.entity.StagePO;
import com.cnap.db.mapper.StageMapper;
import com.cnap.model.stage.StageEnum2;
import com.cnap.model.stage.UpdateStageParam;
import com.cnap.service.StageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <类文件描述>
 *
 * @author xc
 **/
@Service
public class StageServiceImpl implements StageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(StageServiceImpl.class);
    private final StageMapper stageMapper;

    @Autowired
    public StageServiceImpl(StageMapper stageMapper) {
        this.stageMapper = stageMapper;
    }

    @Override
    public boolean isPreviousStageFinished(UpdateStageParam updateStageParam) {
        String cloudType = updateStageParam.getCloudScope();
        String arch = updateStageParam.getArch();

        StageEnum2 firstStage = StageEnum2.getFirstStageEnum2ByCloudTypeAndArch(cloudType, arch);

        // 1-1 已经是第一阶段，不需要判断
        if (!firstStage.getDetailType().equals(updateStageParam.getDetailType())) {
            // 1. 先查询当前阶段
            StageEnum2 current = StageEnum2.getStageEnum2WithParam(updateStageParam.getDetailType(), cloudType, arch);
            // 2. 获取前一阶段
            StageEnum2 previous = current.previous();

            StagePO previousStage = stageMapper.selectStageByServiceIdAndDetailType2(updateStageParam.getSvcId(),
                    previous.getDetailType(), updateStageParam.getCloudScope(), updateStageParam.getArch());

            if (Objects.isNull(previousStage)) {
                LOGGER.error("[pub-cpu]: the previous stage has not been completed, param={}", updateStageParam);
                return true;
            }
        }
        return false;
    }


    @Override
    public void deleteNextStages(String svcId, String detailType, String cloudType, String arch) {
        if (StageEnum2.FOUR_TOW_PUBLIC_CPU.getDetailType().equals(detailType) ||
                StageEnum2.FOUR_TOW_PUBLIC_GPU.getDetailType().equals(detailType) ||
                StageEnum2.UNKNOWN.getDetailType().equals(detailType)) {
            LOGGER.info("svcId={}, detail-type={}, clout-type={}, arch={}, just return", svcId, detailType, cloudType, arch);
            return;
        }

        StageEnum2 current = StageEnum2.getStageEnum2WithParam(detailType, cloudType, arch);
        // 每种可能的4-2已经在前置判断处理了，所以此处不会有从公有云cpu下一个到公有云gpu的情况
        StageEnum2 next = current.next();

        StagePO nextPo = stageMapper.selectStageByServiceIdAndDetailType2(svcId, detailType, cloudType, arch);
        if (Objects.isNull(nextPo)) {
            LOGGER.info("svcId={}, detail-type={}, cloud type={}, arch={} has no next stage, just return", svcId, detailType, cloudType, arch);
            return;
        }

        stageMapper.deleteStageById(nextPo.getId());
        LOGGER.warn("delete next stage, svcId={}, detail-type={}, cloud-type={}, arch={}", svcId, nextPo.getDetailType(), cloudType, arch);

        deleteNextStages(svcId, next.getDetailType(), cloudType, arch);
    }
}
